package com.meituan.catering.management.order.dao.converter;

import com.meituan.catering.management.order.biz.model.request.SearchCateringOrderBizRequest;
import com.meituan.catering.management.order.dao.model.request.SearchCateringOrderDataRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Believe
 * Date 2022/5/27 10:29
 */
public class SearchCateringOrderConverter {

    public static SearchCateringOrderDataRequest toSearchCateringOrderDataRequest(SearchCateringOrderBizRequest request) {
        if (request == null) {
            return null;
        }
        SearchCateringOrderDataRequest searchCateringOrderDataRequest = new SearchCateringOrderDataRequest();
        searchCateringOrderDataRequest.setPageIndex(request.getPageIndex() - 1);
        searchCateringOrderDataRequest.setPageSize(request.getPageSize());
        searchCateringOrderDataRequest.setCondition(toCondition(request.getCondition()));
        searchCateringOrderDataRequest.setSortFields(toSortField(request.getSortFields()));
        return searchCateringOrderDataRequest;
    }

    public static List<SearchCateringOrderDataRequest.SortField> toSortField(List<SearchCateringOrderBizRequest.SortField> sortFields) {
        List<SearchCateringOrderDataRequest.SortField> fields = new ArrayList<>();
        SearchCateringOrderDataRequest.SortField defaultField = new SearchCateringOrderDataRequest.SortField("last_modified_at","desc");
        if (sortFields.isEmpty()){
            fields.add(defaultField);
            return fields;
        }
        for (SearchCateringOrderBizRequest.SortField sortField : sortFields) {
            SearchCateringOrderDataRequest.SortField field = new SearchCateringOrderDataRequest.SortField();
            field.setField(sortField.getField());
            field.setAsc(sortField.getAsc());
            fields.add(field);
        }
        return fields;
    }

    public static SearchCateringOrderDataRequest.Condition toCondition(SearchCateringOrderBizRequest.Condition condition) {
        SearchCateringOrderDataRequest.Condition conditionData = new SearchCateringOrderDataRequest.Condition();
        conditionData.setStatus(condition.getStatus());
        conditionData.setTableNo(condition.getTableNo());
        conditionData.setCustomerCount(condition.getCustomerCount());
        conditionData.setTotalPrice(toTotalPriceRange(condition.getTotalPrice()));
        return conditionData;
    }

    public static SearchCateringOrderDataRequest.TotalPriceRange toTotalPriceRange(SearchCateringOrderBizRequest.TotalPriceRange totalPriceRange) {
        SearchCateringOrderDataRequest.TotalPriceRange totalPriceRangeData = new SearchCateringOrderDataRequest.TotalPriceRange();
        totalPriceRangeData.setFrom(totalPriceRange.getFrom());
        totalPriceRangeData.setTo(totalPriceRange.getTo());
        return totalPriceRangeData;
    }
}
